#include "main.h"
#include "systick.h"

static vu32 DelayTime;
static vu32 tick;

//systick初始化
void SYSTICK_Init(void)
{
  /* SYSTICK configuration */
  SYSTICK_ClockSourceConfig(SYSTICK_SRC_STCLK);       // Default : CK_AHB/8
  SYSTICK_SetReloadValue(SystemCoreClock / 8 / 1000); // (CK_AHB/8/1000) = 1ms on chip
  SYSTICK_IntConfig(ENABLE);                          // Enable SYSTICK Interrupt
  SYSTICK_CounterCmd(SYSTICK_COUNTER_CLEAR);
  SYSTICK_CounterCmd(SYSTICK_COUNTER_ENABLE);
}

void Delay_ms(u32 nTime)
{
  DelayTime = nTime;
  while(DelayTime != 0);
}

u32 tick_get(void)
{
  return tick;
}

u8 tick_istimeout(tick_t *tick)
{
  if(SYS_TICKDIFF(tick->tick_num,tick_get()) >= tick->tick_time)
  {
    tick->tick_num = tick_get();
    return true;
  }
  return false;
}

void tick_refresh(tick_t *tick)
{
	tick->tick_num = tick_get();
}

void Systick_callback(void)
{
  //系统tick更新
  tick++;

  //软件定时器处理函数
  TimerMsHander();

  //延时函数处理
  if(DelayTime != 0)
  {
    DelayTime--;
  }
}
